package per.hx.bs.yw.serviceimpl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import per.hx.bs.entity.sys.SysJobDO;
import per.hx.bs.entity.sys.SysUserDO;
import per.hx.bs.entity.yw.YwAppraisalRecordDO;
import per.hx.bs.entity.yw.YwSettlementDO;
import per.hx.bs.sys.dao.SysUserDao;
import per.hx.bs.sys.dao.SysUserJobDao;
import per.hx.bs.util.DateUtils;
import per.hx.bs.vo.CountCheckUserVO;
import per.hx.bs.yw.dao.YwAppraisalRecordDao;
import per.hx.bs.yw.dao.YwSettlementDao;

import java.util.*;

/**
 * @author HuXing
 * @date 2018/4/30  18:59
 */
@Transactional(rollbackFor = Exception.class)
@RestController
@RequestMapping(value = "yw/settlement")
public class YwSettlementApiServiceImpl {

    private final
    YwSettlementDao settlementDao;
    private final
    YwAppraisalRecordDao recordDao;

    private final
    SysUserJobDao userJobDao;
    private final
    SysUserDao userDao;

    @Autowired
    public YwSettlementApiServiceImpl(YwSettlementDao settlementDao, YwAppraisalRecordDao recordDao, SysUserJobDao userJobDao, SysUserDao userDao) {
        this.settlementDao = settlementDao;
        this.recordDao = recordDao;
        this.userJobDao = userJobDao;
        this.userDao = userDao;
    }

    /**
     * 结算
     *
     * @param userId     结算用户
     * @param settlement 结算时间
     * @return 影响行数
     */
    @PostMapping(value = "/{userId}")
    public Integer settlement(@PathVariable("userId") Long userId, @RequestParam("settlement") String settlement,
                              @RequestParam("name") String name) {

        Map<String, Object> map = new HashMap<>(3);
        map.put("userId", userId);
        map.put("isJs", 0);
        SysJobDO jobDO = userJobDao.selectJobByUserId(userId);
        SysUserDO userDO = userDao.selectById(userId);
        YwSettlementDO settlementDO = new YwSettlementDO();
        settlementDO.setUserId(userId);
        settlementDO.setUserName(userDO.getName());
        settlementDO.setJsJobName(jobDO.getJobName());
        settlementDO.setJsJobMoney(jobDO.getBaseMoney());
        settlementDO.setJsUserName(name);
        settlementDO.setCreateDate(new Date());
        EntityWrapper<YwAppraisalRecordDO> ew = new EntityWrapper<>();
        int lengthDate = 15;
        if (settlement.length() < lengthDate) {
            map.put("searchDate", settlement);
            ew.where("DATE_FORMAT(create_date, '%Y-%m') ={0}", settlement);
            settlementDO.setJsStartDate(settlement + "-01");
            settlementDO.setJsEndDate(DateUtils.formatDate(new Date()));
        } else {
            String[] date = settlement.split(" - ");
            String searchStartDate = date[0];
            String searchEndDate = date[1];
            map.put("searchStartDate", searchStartDate);
            map.put("searchEndDate", searchEndDate);
            ew.between("create_date", searchStartDate, searchEndDate);
            settlementDO.setJsStartDate(searchStartDate);
            settlementDO.setJsEndDate(searchEndDate);
        }

        CountCheckUserVO checkUserVO = recordDao.selectCheckUserVO(map);
        if (null != checkUserVO && null != checkUserVO.getMoney()) {
            settlementDO.setJsAdd(checkUserVO.getMoneyAdd());
            settlementDO.setJsReduce(checkUserVO.getMoneyReduce());
            settlementDO.setJsMoney(checkUserVO.getMoney());
        } else {
            return 100000;
        }
        settlementDO.setMoney(jobDO.getBaseMoney() + settlementDO.getJsMoney());
        settlementDao.insert(settlementDO);

        ew.eq("user_id", userId);
        YwAppraisalRecordDO recordDO = new YwAppraisalRecordDO();
        recordDO.setIsJs(1);
        recordDO.setIsJsId(settlementDO.getId());
        return recordDao.update(recordDO, ew);
    }

    /**
     * 统计 部门结算
     *
     * @param map 调价
     * @return 结果
     */
    @PostMapping(value = "/listAsDept")
    public List<YwSettlementDO> listAsDept(@RequestBody Map<String, Object> map) {
        return settlementDao.listAsDept(map);
    }

    /**
     * 统计个人信息
     *
     * @param deptId     部门id
     * @param searchDate 时间
     * @return 结果
     */
    @GetMapping(value = "/listAsUser")
    public List<YwSettlementDO> listAsUser(@RequestParam("deptId") Long deptId, @RequestParam("searchDate") String searchDate) {
        Map<String, Object> map = new HashMap<>(1);
        map.put("deptId", deptId);
        List<SysUserDO> userDOS = userDao.selectTreeByMap(map);
        if (userDOS == null || userDOS.isEmpty()) {
            return null;
        }
        List<Long> list = new ArrayList<>();
        for (SysUserDO userDO : userDOS) {
            list.add(userDO.getUserId());
        }
        EntityWrapper<YwSettlementDO> ew = new EntityWrapper<>();
        ew.where("DATE_FORMAT(create_date, '%Y-%m') ={0}", searchDate);
        ew.in("user_id", list);
        return settlementDao.selectList(ew);
    }

    /**
     * 通过id获取信息
     *
     * @param id id
     * @return 结果
     */
    @GetMapping(value = "/{id}")
    public YwSettlementDO selectOne(@PathVariable("id") Long id) {
        return settlementDao.selectById(id);
    }

}
